home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap05 / CtlDemo2 / CtlDemo2.cpp next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  3.3 KB  |  116 lines

  1. //***********************************************************************
  2. //
  3. //  CtlDemo2.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "CtlDemo2.h"
  9.  
  10. #define IDC_STDEDIT 100
  11. #define IDC_NUMEDIT 101
  12.  
  13. CMyApp myApp;
  14.  
  15. /////////////////////////////////////////////////////////////////////////
  16. // CMyApp member functions
  17.  
  18. BOOL CMyApp::InitInstance ()
  19. {
  20.     m_pMainWnd = new CMainWindow;
  21.     m_pMainWnd->ShowWindow (m_nCmdShow);
  22.     m_pMainWnd->UpdateWindow ();
  23.     return TRUE;
  24. }
  25.  
  26. /////////////////////////////////////////////////////////////////////////
  27. // CMainWindow message map and member functions
  28.  
  29. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  30.     ON_WM_CREATE ()
  31. END_MESSAGE_MAP ()
  32.  
  33. CMainWindow::CMainWindow ()
  34. {
  35.     CString strWndClass = AfxRegisterWndClass (
  36.         0,
  37.         myApp.LoadStandardCursor (IDC_ARROW),
  38.         (HBRUSH) (COLOR_3DFACE + 1),
  39.         myApp.LoadStandardIcon (IDI_APPLICATION)
  40.     );
  41.  
  42.     Create (strWndClass, "CtlDemo2");
  43. }
  44.  
  45. int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
  46. {
  47.     if (CFrameWnd::OnCreate (lpcs) == -1)
  48.         return -1;
  49.  
  50.     CClientDC dc (this);
  51.     int nHeight = -((dc.GetDeviceCaps (LOGPIXELSY) * 8) / 72);
  52.  
  53.     m_font.CreateFont (nHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
  54.         DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
  55.         DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
  56.  
  57.     CFont* pOldFont = dc.SelectObject (&m_font);
  58.     TEXTMETRIC tm;
  59.     dc.GetTextMetrics (&tm);
  60.     m_cxChar = tm.tmAveCharWidth;
  61.     m_cyChar = tm.tmHeight + tm.tmExternalLeading;
  62.     dc.SelectObject (pOldFont);
  63.  
  64.     int x1 = m_cxChar * 2;
  65.     int x2 = m_cxChar * 24;
  66.     int x3 = m_cxChar * 25;
  67.     int x4 = m_cxChar * 50;
  68.  
  69.     int y1 = m_cyChar;
  70.     int y2 = (m_cyChar * 11) / 4;
  71.     int y3 = m_cyChar * 4;
  72.     int y4 = (m_cyChar * 23) / 4;
  73.     int dy = m_cyChar / 2;
  74.  
  75.     m_ctlLabel1.Create ("&Standard edit control", WS_CHILD | WS_VISIBLE |
  76.         SS_LEFT, CRect (x1, y1 + dy, x2, y2), this);
  77.  
  78.     m_ctlStdEdit.CreateEx (WS_EX_CLIENTEDGE, "edit", NULL,
  79.         WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL,
  80.         x3, y1, x4 - x3, y2 - y1, m_hWnd, (HMENU) IDC_STDEDIT, NULL);
  81.  
  82.     m_ctlLabel2.Create ("&Numeric edit control", WS_CHILD | WS_VISIBLE |
  83.         SS_LEFT, CRect (x1, y3 + dy, x2, y4), this);
  84.  
  85.     m_ctlNumEdit.CreateEx (WS_EX_CLIENTEDGE, "edit", NULL,
  86.         WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL,
  87.         x3, y3, x4 - x3, y4 - y3, m_hWnd, (HMENU) IDC_NUMEDIT, NULL);
  88.  
  89.     m_ctlLabel1.SetFont (&m_font, FALSE);
  90.     m_ctlLabel2.SetFont (&m_font, FALSE);
  91.     m_ctlStdEdit.SetFont (&m_font, FALSE);
  92.     m_ctlNumEdit.SetFont (&m_font, FALSE);
  93.     return 0;
  94. }
  95.  
  96. BOOL CMainWindow::PreTranslateMessage (MSG* pMsg)
  97. {
  98.     return ::IsDialogMessage (m_hWnd, pMsg);
  99. }
  100.  
  101. /////////////////////////////////////////////////////////////////////////
  102. // CNumEdit message map and member functions
  103.  
  104. BEGIN_MESSAGE_MAP (CNumEdit, CEdit)
  105.     ON_WM_CHAR ()
  106. END_MESSAGE_MAP ()
  107.  
  108. void CNumEdit::OnChar (UINT nChar, UINT nRepCnt, UINT nFlags)
  109. {
  110.     if (((nChar >= '0') && (nChar <= '9')) ||
  111.         (nChar == VK_BACK) || (nChar == '(') || (nChar == ')') ||
  112.         (nChar == '-') || (nChar == ' '))
  113.  
  114.         CEdit::OnChar (nChar, nRepCnt, nFlags);
  115. }
  116.